//根据指定的filed返回对应的index
function findIndexByField(arr,field,value){
    var rs = -1;
    for(let i=0,len = arr.length;i<len;i++){
        let item = arr[i];
        if(item[field] === value){
            rs = i;
            break;
        }
    }
    return rs;
}


export default {
    state: {
        /**
         * 存储缓存数据集合 
         * 参考可是，不固定
         * {
         *  active:'',//当前激活元素
         *  page_no:'',//当前分页数，
         *  list:[],  //列表数据
         * }
         */
       map:{}
    },
    mutations: {
        add(state,payload){
            state.map[payload.key] = payload.value;
        },
        delete(state,key){
            if(state.map[key]){
                delete state.map[key];
            }
        },
        deleteItemBykey(state,payload){
            if(state.map[payload.key]){
                delete state.map[payload.key][payload.field];
            }
        },
        deleteFieldByKey(state,payload){
            if(state.map[payload.key]){
                let index = findIndexByField(state.map[payload.key].list,payload.field,payload.value);
                if(index !== -1){
                    state.map[payload.key].list.splice(index,1);
                }
            }
        },
        updateFieldByKey(state,payload) {
            if(state.map[payload.key]){
                state.map[payload.key][payload.field] = payload.value;
            }
        }
    }
}